package com.safeneeds.linguimana.entity.dao.impl;

import java.util.ArrayList;
import java.util.List;

import com.safeneeds.linguimana.dto.StarDataStorePort;
import com.safeneeds.linguimana.entity.PortGlutOnduty;
import com.safeneeds.linguimana.entity.PortGlutStar;
import com.safeneeds.linguimana.entity.dao.IPortGlutStarDao;
import com.safeneeds.util.base.BaseHibernateDao;
import com.safeneeds.util.date.DateUtil;
import com.safeneeds.util.other.StringUtil;
import com.safeneeds.util.page.PageInfo;

public class PortGlutStarDao extends BaseHibernateDao<PortGlutStar, String> implements IPortGlutStarDao{

	@Override
	public List<PortGlutStar> getStar() {
		String hsql="from PortGlutStar order by time desc";
		return this.getEntitiesPage(hsql, 1, 2);
	}

	@Override
	public PageInfo listStarPageDao(PageInfo pageInfo,
			StarDataStorePort starDataStorePort) {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
		String hsql = "SELECT * FROM PORT_GLUT_STAR ";
		String countSql = "SELECT COUNT(*) FROM PORT_GLUT_STAR";
		
		String whereSql = "";
		String orderSql = " ORDER BY TIME desc";
		
		if(starDataStorePort !=null){		
			whereSql += " where 1 = 1";
			//获取最美警星称号
			if(!StringUtil.isEmpty(starDataStorePort.getTitle())){
				whereSql += " and TITLE like '%" + StringUtil.addQuote(starDataStorePort.getTitle()) + "%'";
			}
			//获取最美警星称号名称
			if(!StringUtil.isEmpty(starDataStorePort.getName())){
				whereSql += " and NAME like '%" + StringUtil.addQuote(starDataStorePort.getName()) + "%'";
			}
			//时间
			if(!StringUtil.isEmpty(starDataStorePort.getTimestart()) && !StringUtil.isEmpty(starDataStorePort.getTimeend())){//有开始时间和结束时间
				if(DateUtil.compareDateYYYYMMDD(starDataStorePort.getTimestart(), starDataStorePort.getTimeend())){//开始时间大于结束时间
					whereSql += " and TIME >= to_timestamp('" + starDataStorePort.getTimeend() + " 00:00:00', 'yyyy-MM-dd HH24:MI:SS')"
							 + " and TIME <= to_timestamp('" + starDataStorePort.getTimestart() + " 23:59:59', 'yyyy-MM-dd HH24:MI:SS')";
				}else{
					whereSql += " and TIME <= to_timestamp('" + starDataStorePort.getTimeend() + " 23:59:59', 'yyyy-MM-dd HH24:MI:SS')"
							 + " and TIME >= to_timestamp('" + starDataStorePort.getTimestart() + " 00:00:00', 'yyyy-MM-dd HH24:MI:SS')";
				}
			}else if(!StringUtil.isEmpty(starDataStorePort.getTimestart())){//只有开始时间
				whereSql += " and TIME >= to_timestamp('" + starDataStorePort.getTimestart() + " 00:00:00', 'yyyy-MM-dd HH24:MI:SS')";
			}else if(!StringUtil.isEmpty(starDataStorePort.getTimeend())){//只有结束时间
				whereSql += " and TIME <= to_timestamp('" + starDataStorePort.getTimeend() + " 23:59:59', 'yyyy-MM-dd HH24:MI:SS')";
			}
		}
		pageInfo.setEntityClass(PortGlutStar.class);
		pageInfo.setListHbSql(hsql+whereSql+orderSql);
		pageInfo.setTotalCountHbSQL(countSql+whereSql);
		return pageInfo;
	}

	@Override
	public PortGlutStar getStarByStarId(String starId) {
		// TODO Auto-generated method stub
		return this.findById(starId);
	}

	@Override
	public void deleteStarByIdDao(String ids) {
		// TODO Auto-generated method stub
		this.deleteEntityById(ids);
	}

	@Override
	public void addStarDao(PortGlutStar portGlutStar) {
		// TODO Auto-generated method stub
		this.saveEntity(portGlutStar);
	}

	@Override
	public void saveOrUpdateStarDao(PortGlutStar portGlutStar) {
		// TODO Auto-generated method stub
		this.updateEntity(portGlutStar);
	}

}
